#include "main_window.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>

void MainWindow::decrease_prograss(void)
{
    int val = page1_progbar->value();
    printf("decrease: %d => %d.\n", val, val - 1);
    val--;
    page1_progbar->setValue(val);
}

void MainWindow::increase_prograss(void)
{
    int val = page1_progbar->value();
    printf("increase: %d => %d.\n", val, val + 1);
    val++;
    page1_progbar->setValue(val);
}

void MainWindow::create_page1(void)
{
    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->setSpacing(2); /* 设置控件间距 */
    vbl->setMargin(4);  /* 设置边距 */

    page1_progbar = new QProgressBar();
    page1_progbar->resetFormat();
    page1_progbar->setMinimum(0);
    page1_progbar->setMaximum(10);
    page1_progbar->setValue(4);
    page1_progbar->setFixedHeight(12);
    page1_progbar->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    vbl->addWidget(page1_progbar);

    QHBoxLayout *hbl1 = new QHBoxLayout();
    vbl->addLayout(hbl1, 3);
    QPushButton *pbtn_dec = new QPushButton("&decrease");
    QPushButton *pbtn_inc = new QPushButton("&increase");
    pbtn_dec->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    pbtn_inc->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    hbl1->addWidget(pbtn_dec, 1);
    hbl1->addWidget(pbtn_inc, 1);
    connect(pbtn_dec, &QPushButton::pressed,
            this, &MainWindow::decrease_prograss);
    connect(pbtn_inc, &QPushButton::pressed,
            this, &MainWindow::increase_prograss);

    QHBoxLayout *hbl2 = new QHBoxLayout();
    vbl->addLayout(hbl2, 3);
    hbl2->setSpacing(2); /* 设置控件间距 */
    hbl2->setMargin(4);  /* 设置边距 */
    QPushButton *pbtn_pre = new QPushButton("&prepare");
    QPushButton *pbtn_run = new QPushButton("&run");
    QPushButton *pbtn_stp = new QPushButton("&stop");
    pbtn_pre->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    pbtn_run->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    pbtn_stp->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    hbl2->addWidget(pbtn_pre, 1);
    hbl2->addWidget(pbtn_run, 1);
    hbl2->addWidget(pbtn_stp, 1);
    connect(pbtn_pre, &QPushButton::pressed,
            this, &MainWindow::prepare_progress);
    connect(pbtn_run, &QPushButton::pressed, this, &MainWindow::run_progress);
    connect(pbtn_stp, &QPushButton::pressed, this, &MainWindow::stop_progress);


    printf("Create page 1.\n");
    QWidget *page1 = new QWidget();
    page1->setLayout(vbl);
    tab_widget->addTab(page1, "&1-Basic");
}


